package com.masterclient.app.modules.goods.adapter;

import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.masterclient.app.R;
import com.masterclient.app.modules.goods.ImageGalleryDialog;
import com.eoner.baselibrary.bean.goods.CommentMessage;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.ViewTransformUtil;

import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleWithBorderTransformation;

/**
 * 作者：sean
 * 创建时间：2017/8/21 20:17
 * 功能说明：
 */

public class GoodsCommentAdapter extends BaseQuickAdapter<CommentMessage,BaseViewHolder>{

    int width;

    public GoodsCommentAdapter() {
        super(R.layout.item_good_comment);
        width = (int)((ScreenUtils.getScreenWidth()-ScreenUtils.dp2px(108))/3f);
    }

    @Override
    protected void convert(BaseViewHolder helper, CommentMessage item) {
        helper.setText(R.id.tv_username,item.getNick_name());
        helper.setText(R.id.tv_time,item.getDay_at());
        ViewTransformUtil.glideImageView(mContext,item.getHeadimgurl(),(ImageView) helper.getView(R.id.iv_head),
                new CropCircleWithBorderTransformation(0, 0x00FFFFFF),R.mipmap.user_head_default_icon,
                ScreenUtils.dp2px(44),ScreenUtils.dp2px(44));


        GridLayout gridLayout = helper.getView(R.id.gridLayout);
        LinearLayout llReply = helper.getView(R.id.ll_reply);
        TextView tv_trangle = helper.getView(R.id.tv_trangle);
        TextView tv_reply = helper.getView(R.id.tv_reply);
        TextView tv_sku = helper.getView(R.id.tv_sku);
        TextView tv_comment = helper.getView(R.id.tv_comment);

        if(TextUtils.isEmpty(item.getAttributes())){
            tv_sku.setVisibility(View.GONE);
        }else{
            tv_sku.setText(item.getAttributes());
            tv_sku.setVisibility(View.VISIBLE);
        }

        if(TextUtils.isEmpty(item.getDetail())){
            tv_comment.setVisibility(View.GONE);
        }else{
            tv_comment.setText(item.getDetail());
            tv_comment.setVisibility(View.VISIBLE);
        }


        if(TextUtils.isEmpty(item.getReply_detail())){
            llReply.setVisibility(View.GONE);
        }else {
            llReply.setVisibility(View.VISIBLE);
            tv_reply.setText(item.getReply_detail());
            tv_trangle.setTypeface(Typeface.createFromAsset(mContext.getResources().getAssets(), "fonts/iconfont.ttf"));
        }


        List<String> list = item.getImages();
        gridLayout.removeAllViews();
        if(list!=null){
            for(int i = 0;i<list.size();i++){
                String img = list.get(i);
                ImageView imageView = new ImageView(mContext);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//                imageView.setTag(i+"");
                GlideUtil.displayImage(mContext,img,imageView,R.mipmap.defaults);

                gridLayout.addView(imageView);
                GridLayout.LayoutParams params = new GridLayout.LayoutParams(new ViewGroup.LayoutParams( width, width));
//                params.leftMargin = 6;
                if (i%3 !=2) {
                    params.rightMargin = ScreenUtils.dp2px(12);
                }
                if (i/3 != (list.size()-1)/3) {
                    params.bottomMargin = ScreenUtils.dp2px(12);
                }
                imageView.setLayoutParams(params);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ImageGalleryDialog imageGallery = new ImageGalleryDialog(mContext, list);
                        imageGallery.show();
                        imageGallery.setCurrentPage(list.indexOf(img));
                    }
                });
            }
        }
    }
}
